# Create your views here.
from user import * 
from glue.forum import *
from glue.models import *
from django.core.urlresolvers import reverse
# NOTICE: This is the top level module, do not import this file.

def FBApp(request):
  home_t = loader.get_template('glue/fbapp.html')
  c = Context({'REQUEST':request.REQUEST,'CSS_ROOT':'default','SESSION':request.session})
  return HttpResponse(home_t.render(c))

def FBLogin(request):
  login_t = loader.get_template('glue/fblogin.html')
  c = Context({'SESSION':request.session})
  return HttpResponse(login_t.render(c))

@browser_prefix
def PersonalCollection(request):
  user = GetUsr(request)
  if (user == "GUEST"):
    return redirect("/glue/login/?next="+reverse("glue.views.PersonalCollection",args=()))
  else:
    user = User.objects.get(username = user)
    collections = ServicesOfUser(user)
    collect_t = loader.get_template('glue/collection.html')
    c = RequestContext(request,{'COLLECTIONS':collections,'SESSION':request.session,'CSS_ROOT':'default'})
    response = HttpResponse(collect_t.render(c),mimetype = "text/html")
    return response

@browser_prefix
def RecommendCollection(request):
    isIphone = (lambda x:'iPhone' in x or False)(request.META['HTTP_USER_AGENT'])
    isAndroid = (lambda x:'Android' in x or False)(request.META['HTTP_USER_AGENT'])
    if(isIphone):
      return redirect('/mobile/hot_clubs')
    '''if(isAndroid):
      return redirect('/android_map')'''
    user = User.objects.get(username = 'SYSTEM')
    collections = ServicesOfUser(user)
    collect_t = loader.get_template('glue/mark.html')
    c = RequestContext(request,{'COLLECTIONS':collections,'SESSION':request.session,'CSS_ROOT':'default'})
    response = HttpResponse(collect_t.render(c),mimetype = "text/html")
    return response

@browser_prefix
def MyServices(request):
  user = GetUsr(request)
  if (user == "GUEST"):
    return redirect("/glue/login/?next="+reverse("glue.views.MyServices",args=()))
  else:
    user = User.objects.get(username = user)
    services = ServiceCore.objects.filter(email = user.email)
    collect_t = loader.get_template('glue/myservices.html')
    c = RequestContext(request,{'SERVICES':services,'SESSION':request.session, 'CSS_ROOT':'default'})
    response = HttpResponse(collect_t.render(c),mimetype = "text/html")
    return response

@browser_prefix
def ShopList(request):
    services = ServiceCore.objects.all
    collect_t = loader.get_template('glue/shoplist.html')
    c = RequestContext(request,{'SERVICES':services,'SESSION':request.session, 'CSS_ROOT':'default'})
    response = HttpResponse(collect_t.render(c),mimetype = "text/html")
    return response

@browser_prefix
def Mark(request):
    services = ServiceCore.objects.all()
    mark_t = loader.get_template('glue/mark.html')
    c = RequestContext(request,{'SERVICES':services,'CSS_ROOT':'default','SESSION':request.session})
    response = HttpResponse(mark_t.render(c),mimetype = "text/html")
    return response

def Users(request):
  users_t  = loader.get_template('usrs/allusers.html');
  users = User.objects.all()
  c = RequestContext(request,{'USERS':users,'CSS_ROOT':'default'})
  return HttpResponse(users_t.render(c))

@browser_prefix
def Data(request,sname,path):
  aut = HasAuthority(request,sname)
  command_error = {}
  if (aut['r'] == False):
      command_error['AUTHORITY'] = 'NO_AUTHORITY'
      return GeneralXMLResponse(request,command_error)
  data = aut['s']
  data_t  = loader.get_template('glue/data.html');
  c = RequestContext(request,{'SERVICE':data,'PATH':path})
  return HttpResponse(data_t.render(c))

def Verify(request):
  return HttpResponse("google-site-verification: googlea92d5357550656a3.html");

def Verify2(request):
  data_sitemap = open("/var/www/sydney-scent/sitemap.xml","rb").read();
  return HttpResponse(data_sitemap, mimetype="text/xml");

def Verify3(request):
  data_sitemap = open("/var/www/sydney-scent/robots.txt","rb").read();
  return HttpResponse(data_sitemap, mimetype="text/txt");

@browser_prefix
def Mission(request):
  main_t = loader.get_template('zoyoe/zoyoe.html')
  c = RequestContext(request,{'REQUEST':request.REQUEST,'CSS_ROOT':'default','SESSION':request.session})
  return HttpResponse(main_t.render(c))

@browser_prefix
def StartUsing(request):
  main_t = loader.get_template('zoyoe/startusing.html')
  c = RequestContext(request,{'REQUEST':request.REQUEST,'CSS_ROOT':'default','SESSION':request.session})
  return HttpResponse(main_t.render(c))

@browser_prefix
def Program(request):
  user = GetUsr(request)
  if (user == "GUEST"):
    return redirect("/glue/login/?next="+reverse("main.glue.views.Program",args=()))
  else:
    main_t = loader.get_template('zoyoe/program.html')
    c = RequestContext(request,{'REQUEST':request.REQUEST,'CSS_ROOT':'default','SESSION':request.session})
    return HttpResponse(main_t.render(c))

@browser_prefix
def Program(request):
  return redirect("/form/")

@browser_prefix
def Support(request):
  return redirect("/form/")
